<?php
$basedir = dirname(__FILE__);

require_once $basedir . '/../classes/minime.php';
require_once $basedir . '/../extlib/Crypt/RSA.php';
require_once $basedir . '/../extlib/Crypt/Random.php';
require_once $basedir . '/../extlib/Crypt/Hash.php';
require_once $basedir . '/../extlib/Math/BigInteger.php';

MiniMe::initialize();

if( count($argv) < 4) {
      echo "\n\tERROR!";
      echo "\n\t\tPlease provide USERNAME TO MESSAGE as parameters to this function.";
      echo "\n\n\tExample:";
      echo "\n\t\t" . 'php blogto.php koski "http://identi.ca/user/568" "Ready to Rock\'n\'Roll! "';
      echo "\n\n";
      exit;
}

$username = $argv[1];
$msgTo    = $argv[2];
$msg      = $argv[3];

if(isset($argv[4]) && isset($argv[5])) {
    $ref = $argv[4];
    $href = $argv[5];    
} else {
    $ref = false;
    $href = false;
}

$userLoader = new Minime_User_Loader(Minime::$conf);
$user = $userLoader->getUser($username);

if(!$user) {
    echo "\n\t\tDude, the username you want to send the message from does not exists!";
    echo "\n\n";
    exit;
}

$recipients['to'] = array($msgTo);
$recipients = Minime_Helpers::parseRecepientsFromMicroblogNote($msg, $recipients);

$receivers = array();

foreach ($recipients['to'] as $msgTo) {
    // Let's discover the dude we want to reply.
    echo "\n\tDiscovering '" . $msgTo . "'s salmon endpoint ... \n\n";
    $discoverer = new Discovery_Uri($msgTo);
    if(!$discoverer->discover()) {
         echo "\n\tDiscovery of '" . $msgTo . "' Failed!\n\n";
         exit;
    }
    
    echo "\n\t\t- Name   : '" . $discoverer->getName() . "'.";
    echo "\n\t\t- Salmon : '" . $discoverer->getSalmonUri() . "'.";
    
    
    $receivers[$discoverer->getDomain()]['salmon_endpoint'] = $discoverer->getSalmonUri();
    $receivers[$discoverer->getDomain()]['receivers'][] = $discoverer->getFeedsAuthorUri();
}

foreach ($receivers as $domain => $receivers) {
    
    echo "\n\tStarting to send salmon to '" . $domain . "'s salmon endpoint ... \n\n";
    $salmon = new Salmon_Message();
    if(!$salmon->createAtomEntryAndSendDoEverythingThisIsTemporarTest($user, $receivers['receivers'], $msg, $receivers['salmon_endpoint'], $ref, $href)) {
        echo "\n\tSalmon post FAILED! (Sorry, log does not yet say why, bad cod here :-( )\n\n";    
        //exit;
    } else {
        echo "\n\t\t- Salmon notice OK.";    
    }
}

// We need to add this to our blog
$atomLoader = new Minime_Atom_Loader(MiniMe::$conf);
$feed = $atomLoader->getFeed($username);

//$feed->addChildElement($entry);
$feed->addChildElement($salmon->generatedAtom);
$feed->updateUpdated();

$atomStorer = new Minime_Atom_Storer(MiniMe::$conf);
$atomStorer->storeAtom($username, $feed);

//Pubsubhubbub Magic - A.K.A a TERRIBLE HACK!!!!
//$copyOfFeed = $feed->createCopyWithoutEntries();
//$copyOfFeed->addChildElement($salmon->generatedAtom);
//
//$subscribersLoader = new Minime_Pubsubhubbub_Subscribers_Loader(Minime::$conf);
//$subid = Pubsubhubbub_Subscribe::createID(Minime_HrefRenderer::getLdrrAtomHref($username, Minime::$conf));
//echo "\n\n\tReady to Send Pubsub subscriptions of node " . Minime_HrefRenderer::getLdrrAtomHref($username, Minime::$conf) . " (" .$subid . ")";
//$subscribers = $subscribersLoader->getSubscribers($subid);
//echo "\n\tData to be send:";
//echo "\n" . $copyOfFeed->asXML();

// Let's not publish this to no-one.
//if($subscribers != null) {
//    foreach ($subscribers->getSubscribers() as $subscriber) {
//        // this is getting bigger and bigger hack!
//        Pubsubhubbub_Publish::publish($subscriber->getCallback(), $subscriber->getSecret(), $copyOfFeed->asXML());   
//    }
//}

echo "\n\n\tDONE!\n";
